package com.haiwei.poi.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * 日期工具类.
 */
@Slf4j
public class HaiweiDateUtil {
	
	private HaiweiDateUtil() {}

    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT1 = "yyyy-MM-dd";

    private static final String DATE_FORMAT_FOR_FILENAME = "yyyyMMddHHmmss";

    public static final String DATE_FORMAT_FOR_MONTH = "yyyy-MM";
    
    public static final String DATE_FORMAT22 = "YYYY/MM/DD";

    private static final String DATE_FORMAT2 = "yyyy/MM/dd";
    
    private static final String DATE_FORMAT3 = "yyyyMMddhhmmss";

    private static final SimpleDateFormat format1 = new SimpleDateFormat(DATE_FORMAT1);

    private static final SimpleDateFormat format2 = new SimpleDateFormat(DATE_FORMAT2);
    
    private static final SimpleDateFormat format3 = new SimpleDateFormat(DATE_FORMAT3);

    private static Date date900 = null;
    private static Date date999 = null;
    
    static{
        try {
            date900 = format1.parse("1900-01-01");
            date999 = format1.parse("9999-01-01");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 在时间的基础上加上多少秒
     * @param add
     * @param s
     * @return
     */
    public static Date addSecend(Date add,int s){
    	Calendar c = Calendar.getInstance();  
    	c.setTime(add);  
    	c.add(Calendar.SECOND, s);
    	return c.getTime();
    }
    
    public static String getDate999(String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date999);
    }
    
    public static String getDate00(String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date900);
    }
    
    /**
     * 获取时间字符串
     * @return
     */
    public static String getDateString(){
        return format3.format(new Date());
    }
    
    /**
     * 当前时间戳
     * @return
     */
    public static Long currentTimeMillis(){
        return System.currentTimeMillis();
    }
    

    /**
     * 校验date时间格式是否OK
     * eg:
     * 1. 2019-01-02 -> true
     * 2. 2019/01/02 -> true
     * 3. 20190102 -> false
     * 4. null -> false
     * 5. "" -> false
     * 6. "123123" -> false
     * @param date
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean volidate(String date) {

        if(date == null || date.trim().length() == 0 ){
            return false;
        }

        try {
            return format1.parse(date) != null;
        } catch (Exception e) {}

        try {
            return format2.parse(date) != null;
        } catch (Exception e) {}

        try {
            return new Date(date) != null;
        } catch (Exception e) {e.printStackTrace();}

        return false;
    }
    
    /**
     * 转换类型
     * @param date
     * @return
     */
    @SuppressWarnings("deprecation")
    public static Date exchange(String date) {

        if(date == null || date.trim().length() == 0 ){
            return null;
        }

        try {
            return format1.parse(date) ;
        } catch (Exception e) {}

        try {
            return format2.parse(date) ;
        } catch (Exception e) {}

        try {
            return new Date(date);
        } catch (Exception e) {e.printStackTrace();}

        return null;
    }




    

    /**
     * 将String转化为时间对象
     *
     * @param str String对象
     * @param pattern yyyy-MM-dd HH:mm:ss
     * @return 时间对象
     */
    private static Date string2date(String str, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            log.error("date SimpleDateFormat error:", e);
        }
        return date;
    }

    /**
     * 按照 标准格式 yyyy-MM-dd HH:mm:ss，string => date
     *
     * @param str String对象
     * @return 时间对象
     */
    public static Date str2date(String str) {
        return string2date(str, DATE_FORMAT);
    }

    public static Date str2date(String str, String pattern) {
        return string2date(str, pattern);
    }

    /**
     * 格式化时间对象，输出String
     *
     * @param date 时间对象
     * @param pattern yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String date2string(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if(date == null){
            return "";
        }
        return sdf.format(date);
    }

    /**
     * 按照 标准格式 yyyy-MM-dd HH:mm:ss，date => string
     *
     * @param date 时间对象
     * @return String
     */
    public static String date2str(Date date) {
        return date2string(date, DATE_FORMAT);
    }

    /**
     * 获取当前时间，以String 格式输出，yyyy-MM-dd HH:mm:ss
     *
     * @return 当前时间
     */
    public static String now() {
        Calendar cNow = Calendar.getInstance();
        Date dNow = cNow.getTime();
        return date2string(dNow, DATE_FORMAT);
    }

    /**
     * 获取当前时间，以String 格式输出，yyyyMMddHHmmss
     *
     * @return String
     */
    public static String getCurrentTime4FileName() {
        Calendar cNow = Calendar.getInstance();
        Date dNow = cNow.getTime();
        return date2string(dNow, DATE_FORMAT_FOR_FILENAME);
    }

    /**
     * 将时间格式字符串转为毫秒值
     *
     * @param time String时间
     * @return 毫秒值
     */
    public static String stringToSeconds(String time) {
        Date date = string2date(time, DATE_FORMAT);
        if (null != date) {
            return String.valueOf(date.getTime());
        }
        return "";
    }

    /**
     * 获取上月最后一天
     *
     * @return 上月最后一天
     */
    public static String lastDayByMonth() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_FOR_MONTH);
        // 获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        // 设置为1号,当前日期既为本月第一天
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return format.format(cale.getTime());
    }

    /**
     * 获取最近的Ｎ个月份集合
     *
     * @param number Ｎ个月份
     * @return 最近的Ｎ个月份集合
     */
    public static List<String> getNearlyMonths(int number) {
        if (number > 0) {
            SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_FOR_MONTH);
            List<String> nearlyMonths = new ArrayList<>();
            for (int i = 0; i < number; i++) {
                Calendar cale = Calendar.getInstance();
                cale.setTime(new Date());
                cale.add(Calendar.MONTH, -i);
                nearlyMonths.add(format.format(cale.getTime()));
            }
            return nearlyMonths;
        }
        return Collections.emptyList();
    }

    /**
     * 获取参数月份的上一个月份
     *
     * @param month 参数月份
     * @return 参数月份的上一个月份
     */
    public static String getLastMonth(Date month) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_FOR_MONTH);
        Calendar cale = Calendar.getInstance();
        cale.setTime(month);
        cale.add(Calendar.MONTH, -1);
        return format.format(cale.getTime());
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间 HH:mm
     * @param startTime 开始时间 HH:mm
     * @param endTime 结束时间 HH:mm
     * @return
     */
    public static boolean nowTimeHit(String nowTime, String startTime, String endTime) throws ParseException {
        if (nowTime == startTime || nowTime == endTime) {
            return true;
        }

        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date nowDt = df.parse(nowTime);
        Date beginDt = df.parse(startTime);
        Date endDt = df.parse(endTime);
        Calendar CurCal = Calendar.getInstance();
        CurCal.setTime(nowDt);
        Calendar beginCal = Calendar.getInstance();
        beginCal.setTime(beginDt);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDt);
        if (CurCal.before(endCal) && CurCal.after(beginCal)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前日期星期几
     */
    public static String getDayofweek(Date dt){
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

}
